import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class FlyButton extends StatelessWidget {
  final String buttonType; //Outlined,Elevated(default)
  final Color customColor;
  final String shape;
  final String type;
  final String content;
  final bool disable;
  final Function onPressed;
  final bool blockButton;
  final String size;
  final Icon icon;
  const FlyButton(
    this.content, {
    Key key,
    this.buttonType,
    this.customColor = Colors.purple,
    this.shape = "square", //circular--圆角 square--直角(默认)
    this.type, //default ——默认按钮  primary——主要按钮  info——信息按钮  warning——警告按钮  danger——危险按钮 custom——自定义
    this.disable = false,
    this.onPressed,
    this.blockButton = false,
    this.size =
        "normal", // large---超大按钮  big---大号按钮  normal---普通按钮   small---小号按钮  mini---迷你按钮
    this.icon,
  }) : super(key: key);

  static const Color defaultcolor = Colors.blueGrey;
  static const Color primary = Colors.green;
  static const Color info = Colors.blue;
  static const Color warning = Colors.orange;
  static const Color danger = Colors.red;
  static Color disableDanger = danger.withOpacity(0.5);
  static Color disableDefaultcolor = Colors.blueGrey.withOpacity(0.5);
  static Color disablePrimary = Colors.green.withOpacity(0.5);
  static Color disableInfo = Colors.blue.withOpacity(0.5);
  static Color disableWarning = Colors.orange.withOpacity(0.5);

  @override
  Widget build(BuildContext context) {
    return Container(
      child: buttonType == "Outlined"
          ? OutlinedButton(
              child: Text(content),
              style: ButtonStyle(
                  //设置按钮的大小
                  minimumSize: !blockButton
                      ? size == "small"
                          ? MaterialStateProperty.all(
                              Size(double.minPositive, 28))
                          : size == "mini"
                              ? MaterialStateProperty.all(
                                  Size(double.minPositive, 20))
                              : size == "big"
                                  ? MaterialStateProperty.all(
                                      Size(double.minPositive, 44))
                                  : size == "large"
                                      ? MaterialStateProperty.all(
                                          Size(double.minPositive, 52))
                                      : null
                      : MaterialStateProperty.all(Size(double.infinity, 36)),
                  side: MaterialStateProperty.all(BorderSide(
                      color: type == "danger"
                          ? danger
                          : type == "primary"
                              ? primary
                              : type == "info"
                                  ? info
                                  : type == "warning"
                                      ? warning
                                      : type == "default"
                                          ? defaultcolor
                                          : type == "custom"
                                              ? customColor
                                              : Theme.of(context).accentColor,
                      width: 1)),
                  shape: shape == "round"
                      ? MaterialStateProperty.all(RoundedRectangleBorder(
                          borderRadius: BorderRadius.circular(44)))
                      : shape == "square"
                          ? null
                          : null),
              onPressed: () {
                disable ? () {} : onPressed;
              },
            )
          : ElevatedButton(
              child: Container(
                child: Wrap(
                  children: [
                    Icon(
                      Icons.add,
                      size: 16,
                    ),
                    Text(content)
                  ],
                ),
              ),
              style: ButtonStyle(
                  //设置按钮的大小
                  minimumSize: !blockButton
                      ? size == "small"
                          ? MaterialStateProperty.all(
                              Size(double.minPositive, 28))
                          : size == "mini"
                              ? MaterialStateProperty.all(
                                  Size(double.minPositive, 20))
                              : size == "big"
                                  ? MaterialStateProperty.all(
                                      Size(double.minPositive, 44))
                                  : size == "large"
                                      ? MaterialStateProperty.all(
                                          Size(double.minPositive, 52))
                                      : null
                      : MaterialStateProperty.all(Size(double.infinity, 36)),
                  shape: shape == "round"
                      ? MaterialStateProperty.all(RoundedRectangleBorder(
                          borderRadius: BorderRadius.circular(44)))
                      : shape == "square"
                          ? null
                          : null,
                  backgroundColor: MaterialStateColor.resolveWith((states) {
                    if (states.contains(MaterialState.pressed) && !disable) {
                      return null;
                    }

                    return type == "danger"
                        ? (disable ? disableDanger : danger)
                        : type == "primary"
                            ? (disable ? disablePrimary : primary)
                            : type == "info"
                                ? (disable ? disableInfo : info)
                                : type == "warning"
                                    ? (disable ? disableWarning : warning)
                                    : type == "default"
                                        ? (disable
                                            ? disableDefaultcolor
                                            : defaultcolor)
                                        : type == "custom"
                                            ? (disable
                                                ? customColor.withOpacity(0.5)
                                                : customColor)
                                            : Theme.of(context).accentColor;
                  })),
              onPressed: () {
                disable ? () {} : onPressed;
              },
            ),
    );
  }
}
